<?php
require_once "../../helpers/amazon/data.php";                //static data about Amazon (servers, search fields) v3-
require_once "../../helpers/amazon/data_v4.php";             //static data about Amazon (servers, search fields) v4+
require_once "../../helpers/amazon/aws_signed_request.php";  //see: http://mierendo.com/software/aws_signed_query/, please note that I'm using an slightly adapted version of the script: I do my own XML-interpreting

function aws_request($parameters) {

    $public_key = "AKIAJPZ5IQJP2NHO2PNQ";
    $private_key = "wup932aMRjF99xR2xp+IVRaTkNH2PaAPEpuLIXUr";

    $ext = 'com';
    $file_data = $ext;
    ksort($parameters);


    foreach ($parameters as $i => $d) {
        $file_data.='&' . $i . '=' . $d;
    }


    $file = aws_signed_request($ext, $parameters, $public_key, $private_key);  //20090804: sign the request

    $cache_time = 3600;  //60s*60m=1h, in seconds how long a cache entry may be used
    //Use 0 for no caching
    $time0 = time();

    $cachedir = '../../cache/';  //directory where to cache things.

    if (file_exists($cachedir) and is_dir($cachedir)) {
        //Okay: directory already exists
    } else {
        if (mkdir($cachedir)) {
           // echo "cache directory created<br />\n";
        }
        else
            die("can't make cache directory");
    }
    if (is_readable($cachedir) && is_writable($cachedir)) {
        //Okay: directory is readable and writable by the script
    } else {
        die("cache directory is not readable and writable");
    }
    if ($cache_time and $hd = opendir($cachedir)) {
        while ($fn = readdir($hd)) {
            if ($time0 - filemtime($cachedir . $fn) >= $cache_time) {     //is the file too old?
                if (!($fn == '.' or $fn == '..'))
                    unlink($cachedir . $fn);  //remove it
            }
        }
        closedir($hd);
    }

    $cache_file_name = $cachedir . $file_data;
    
    if ($cache_time and //are we caching?
            file_exists($cache_file_name) and //file already in cache? should be readable
            $filecontents = file_get_contents($cache_file_name)) {    //file already in cache?
    } else {
        if ($filecontents = file_get_contents($file)) {             //get the file from Amazon, did we succeed?
            if ($cache_time) {                                      //are we caching?
                file_put_contents($cache_file_name, $filecontents);    //write the cache file
            }
        }
        else
            die(xu("can't get data from Amazon") . ".\n");
    }

    if ($A = simplexml_load_string($filecontents)

        );  //decode the XML to an object
 else
        die("can't decode the XML data");
    return $A;
}
?>